package com.example.android.chartapplication;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.os.Handler;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.view.View;

import java.util.Calendar;

public class ChartClock extends View {
    public ChartClock(Context context) {
        super(context);
    }

    public ChartClock(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        handler.postDelayed(runnable, 1000);
    }

    public ChartClock(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    Handler handler = new Handler();
    Runnable runnable = new Runnable() {
        @Override
        public void run() {
            postInvalidate();
            handler.postDelayed(this, 1000);
        }
    };

    Paint paint = new Paint();
    Paint textPaint = new Paint();
    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        canvas.translate(150, 150);
        paint.setAntiAlias(true);
        paint.setStrokeWidth(3);
        paint.setStrokeCap(Paint.Cap.ROUND);
        textPaint.setAntiAlias(true);

        paint.setStyle(Paint.Style.FILL);
        paint.setColor(Color.WHITE);
        canvas.drawCircle(0, 0, 100, paint);

        paint.setStyle(Paint.Style.STROKE);
        paint.setColor(Color.BLACK);
        canvas.drawCircle(0, 0, 100, paint);

        for (int i = 0; i < 12; i++) {
            float angle = -60 + 30 * i;
            float startX = ChartRadar.makeX(angle, 100);
            float startY = ChartRadar.makeY(angle, 100);
            float stopX = ChartRadar.makeX(angle, (i+1) % 3 != 0 ? 90 : 85);
            float stopY = ChartRadar.makeY(angle, (i+1) % 3 != 0 ? 90 : 85);
            canvas.drawLine(startX, startY, stopX, stopY, paint);

            float textX = ChartRadar.makeX(angle, 80);
            float textY = ChartRadar.makeY(angle, 80);
            canvas.drawText((i+1)+"", textX-5, textY+5, textPaint);
        }
    }

    Paint contentPaint = new Paint();
    @Override
    protected void dispatchDraw(Canvas canvas) {
        super.dispatchDraw(canvas);
        contentPaint.setAntiAlias(true);
        contentPaint.setStyle(Paint.Style.STROKE);
        contentPaint.setStrokeWidth(3);
        contentPaint.setStrokeCap(Paint.Cap.ROUND);

        Calendar time = Calendar.getInstance();

        int hour = time.get(Calendar.HOUR_OF_DAY);
        int minute = time.get(Calendar.MINUTE);
        int second = time.get(Calendar.SECOND);

        contentPaint.setColor(Color.BLACK);
        canvas.drawLine(0, 0, ChartRadar.makeX( -90 + second * 6, 65),
                ChartRadar.makeY(-90 + second * 6, 65), contentPaint);
        contentPaint.setColor(Color.RED);
        canvas.drawLine(0, 0, ChartRadar.makeX(-90 + minute * 6, 55),
                ChartRadar.makeY(-90 + minute * 6, 55), contentPaint);
        contentPaint.setColor(Color.parseColor("#611e1e"));
        canvas.drawLine(0, 0, ChartRadar.makeX(-90 + hour * 30 + minute * 0.5f, 35),
                ChartRadar.makeY(-90 + hour * 30 + minute * 0.5f, 35), contentPaint);
    }
}
